package com.teach;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

public class Test5 {

	public static void main(String[] args) throws Exception {
		copy(new File("d:/upload/img.png"), new File("d:/upload/temp.png"));
		
		com.teach.util.FileUtil.copy(new File("d:/upload/img.png"), new File("d:/upload/temp1.png"));
		
		cn.hutool.core.io.FileUtil.copy(new File("d:/upload/img.png"), new File("d:/upload/temp2.png"),true);//原文件，目标文件，是否覆盖
//		cn.hutool.core.io.IoUtil.copy(in, out)
		cn.hutool.core.io.FileUtil.copy(new File("D:\\upload\\目录04"), new File("D:\\upload\\目录06"), true);//目录拷贝
	}

	public static void copy(InputStream in, OutputStream out) throws IOException {
		try {
			
			byte[] bs = new byte[1024];
			int len = 0;
			int i = 0;
			int size = 0;
			int size1 = in.available();
			while ((len = in.read(bs)) > -1) {
				size += len;
				System.out.format("%5d-%6d:%s\n", i++,len,Arrays.toString(bs));
				out.write(bs,0,len);
			}
			System.out.println("文件大小："+size+" - "+size1);
		} finally {
			try {
				in.close();
			} catch (Exception e) {
			}
			try {
				out.close();
			} catch (Exception e) {
			}
		}
	}

	public static void copy(File srcFile, File targetFile) throws IOException {
		copy(new FileInputStream(srcFile), new FileOutputStream(targetFile));
	}
}
